﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmchucdanh : Form
    {
        
        public frmchucdanh()
        {
            InitializeComponent();

            TV_Tonghop.Themcot("MACD", "Mã Chức Danh", dt_chucdanh, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("TENCD", "Tên Chức Danh", dt_chucdanh, DataGridViewAutoSizeColumnMode.Fill);
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

       

        private void frmchucdanh_Load(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiChucDanh", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_chucdanh.DataSource = dt;
        }

        private void bt_Them_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemChucDanh", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@macd", SqlDbType.Char).Value = Cb_MaCD.Text;
            cmd.Parameters.Add("@tencd", SqlDbType.NVarChar).Value = Txt_TenCD.Text;
            cmd.ExecuteNonQuery();
            frmchucdanh_Load(sender, e);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatChucDanh", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@macd", SqlDbType.Char).Value = Cb_MaCD.Text;
            cmd.Parameters.Add("@tencd", SqlDbType.NVarChar).Value = Txt_TenCD.Text;
            cmd.ExecuteNonQuery();
            frmchucdanh_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaChucDanh", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@macd", SqlDbType.Char).Value = Cb_MaCD.Text;
            cmd.ExecuteNonQuery();
            frmchucdanh_Load(sender, e);
        }


        int dong;
        private void dt_chucdanh_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            Cb_MaCD.Text = dt_chucdanh.Rows[dong].Cells[0].Value.ToString();
            Txt_TenCD.Text = dt_chucdanh.Rows[dong].Cells[1].Value.ToString();
        }

      
       
    }
}
